package com.reporting.yb.module.system.controller.admin.wxusers;

import com.reporting.yb.framework.apilog.core.annotation.ApiAccessLog;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.Valid;
import org.springframework.web.bind.annotation.*;

import jakarta.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;


import java.util.*;
import java.io.IOException;

import com.reporting.yb.framework.common.pojo.PageParam;
import com.reporting.yb.framework.common.pojo.PageResult;
import com.reporting.yb.framework.common.pojo.CommonResult;
import com.reporting.yb.framework.common.util.object.BeanUtils;

import static com.reporting.yb.framework.apilog.core.enums.OperateTypeEnum.EXPORT;
import static com.reporting.yb.framework.common.pojo.CommonResult.success;

import com.reporting.yb.framework.excel.core.util.ExcelUtils;


import com.reporting.yb.module.system.controller.admin.wxusers.vo.*;
import com.reporting.yb.module.system.dal.dataobject.wxusers.WxUsersDO;
import com.reporting.yb.module.system.service.wxusers.WxUsersService;

@Tag(name = "管理后台 - 微信登录 用户")
@RestController
@RequestMapping("/system/wx-users")
@Validated
public class WxUsersController {

    @Resource
    private WxUsersService wxUsersService;

    @PostMapping("/create")
    @Operation(summary = "创建微信登录 用户")
    @PreAuthorize("@ss.hasPermission('system:wx-users:create')")
    public CommonResult<String> createWxUsers(@Valid @RequestBody WxUsersSaveReqVO createReqVO) {
        return success(wxUsersService.createWxUsers(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新微信登录 用户")
    @PreAuthorize("@ss.hasPermission('system:wx-users:update')")
    public CommonResult<Boolean> updateWxUsers(@Valid @RequestBody WxUsersSaveReqVO updateReqVO) {
        wxUsersService.updateWxUsers(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除微信登录 用户")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('system:wx-users:delete')")
    public CommonResult<Boolean> deleteWxUsers(@RequestParam("id") String id) {
        wxUsersService.deleteWxUsers(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得微信登录 用户")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('system:wx-users:query')")
    public CommonResult<WxUsersRespVO> getWxUsers(@RequestParam("id") String id) {
        WxUsersDO wxUsers = wxUsersService.getWxUsers(id);
        return success(BeanUtils.toBean(wxUsers, WxUsersRespVO.class));
    }

    @GetMapping("/page")

    @Operation(summary = "获得微信登录 用户分页")
    @PreAuthorize("@ss.hasPermission('system:wx-users:query')")
    public CommonResult<PageResult<WxUsersRespVO>> getWxUsersPage(@Valid WxUsersPageReqVO pageReqVO) {
        PageResult<WxUsersDO> pageResult = wxUsersService.getWxUsersPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, WxUsersRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出微信登录 用户 Excel")
    @PreAuthorize("@ss.hasPermission('system:wx-users:export')")
    @ApiAccessLog(operateType = EXPORT)

    public void exportWxUsersExcel(@Valid WxUsersPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<WxUsersDO> list = wxUsersService.getWxUsersPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "微信登录 用户.xls", "数据", WxUsersRespVO.class,
                        BeanUtils.toBean(list, WxUsersRespVO.class));
    }

}